package com.han.dao.concrete.product;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;

import com.han.bom.Question;
import com.han.dao.abstraction.product.GenericAnswerDAO;

public class AnswerDAO extends DataAccessObject implements GenericAnswerDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2796032640374739735L;

	public ArrayList<String> retrieveAnswersByQuestion(String questionid)
			throws SQLException, ClassNotFoundException, NamingException {
		ArrayList<String> choices = new ArrayList<String>();
		int qid = 0;
		String choice = "";

		try {
			qid = Integer.parseInt(questionid.replaceAll("[^\\d]", ""));
		} catch (NumberFormatException e) {
			qid = 0;
		}

		try {
			this.connect();
			String select = "SELECT * FROM tbl_choice WHERE question_id=? ORDER BY answer_iscorrect DESC";
			this.preparedStatement = this.connection.prepareStatement(select);
			this.preparedStatement.setInt(1, qid);
			this.resultSet = this.preparedStatement.executeQuery();

			while (this.resultSet.next()) {
				choice = this.resultSet.getString("answer");
				choices.add(choice);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			throw e;
		} finally {
			this.disconnect();
		}

		return choices;
	}

	public boolean addAnswerToQuestion(Question question)
			throws ClassNotFoundException, SQLException, NamingException {
		boolean success = false;
		String insert = "INSERT IGNORE INTO tbl_choice(answer_id,answer,question_id,answer_iscorrect) VALUES(?,?,?,?)";
		int questionid = Integer.parseInt(question.getQuestionID().replaceAll(
				"[^\\d]", ""));
		int i = 0;
		int row = 0;

		try {
			questionid = Integer.parseInt(question.getQuestionID().replaceAll(
					"[^\\d]", ""));
		} catch (NumberFormatException e) {
			questionid = 0;
		}

		try {
			this.connect();
			this.preparedStatement = this.connection.prepareStatement(insert);

			for (String answer : question.getChoices()) {
				String ansid = "" + question.getQuestionID() + (i + 1) + "A";
				this.preparedStatement.setString(1, ansid);
				this.preparedStatement.setString(2, answer);
				this.preparedStatement.setInt(3, questionid);

				if (question.getAnswer().equals(answer)) {
					this.preparedStatement.setBoolean(4, true);
				} else {
					this.preparedStatement.setBoolean(4, false);
				}
				row += this.preparedStatement.executeUpdate();
				i++;
			}

			success = (row == question.getChoices().size()) ? true : false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			throw e;
		} finally {
			this.disconnect();
		}

		return success;
	}

	public boolean updateAnswer(String answerid, String answer,
			boolean iscorrect) throws ClassNotFoundException, SQLException,
			NamingException {
		boolean success = false;
		String update = "UPDATE tbl_choice SET answer = ?,answer_iscorrect=? WHERE answer_id=?";

		try {
			this.connect();
			this.preparedStatement = this.connection.prepareStatement(update);

			this.preparedStatement.setString(1, answer);
			this.preparedStatement.setBoolean(2, iscorrect);
			this.preparedStatement.setString(3, answerid);
			this.preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			throw e;
		} finally {
			this.disconnect();
		}

		return success;
	}
}
